#pragma once

#include "Reactor.hpp"
#include <unistd.h>
#include <fcntl.h>
#include <vector>

void SetNonBlock(int sock)
{
    int fl = fcntl(sock, F_GETFL);
    if(fl < 0)
    {
        std::cout << "设置非阻塞出错" << std::endl;
        return;
    }

    fcntl(sock, F_SETFL, fl | O_NONBLOCK);
}

void SplitSegment(std::string& inbuffer, std::vector<std::string>& tokens, std::string& sep)
{
    while(1)
    {
        std::cout << "inbuffer: " << inbuffer << std::endl;
        // 找到X位置
        size_t pos = inbuffer.find(sep);
        
        if(pos == std::string::npos)
            return;
        tokens.push_back(inbuffer.substr(0, pos));
        inbuffer.erase(0, pos + sep.size());
    }
}

bool Deserialize(std::string &seg, std::string *out1, std::string *out2)
{
    size_t pos = seg.find('+');
    if(pos == std::string::npos)
    {
        return false;
    }

    *out1 = seg.substr(0, pos);
    *out2 = seg.substr(pos + 1);
    return true;
}